<template>
    <van-button
      :class="{
        collected:value
      }"
      :icon="value===1?'good-job':'good-job-o'"
      :loading='loading'
      @click="onLickeArticle"
    ></van-button>
</template>

<script>
// 导入接口
import { likeContentarticle, deleteContentarticle } from '@/api/articles'
export default {
  name: 'LikeArticle',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true

    }
  },
  data () {
    return {
      loading: false
    }
  },
  created () {

  },
  mounted () {

  },
  methods: {
    async onLickeArticle () {
      this.loading = true
      try {
        // 判断点赞
        let status = -1
        if (this.value === 1) {
          // 取消点赞
          await deleteContentarticle(this.articleId)
        } else {
          await likeContentarticle(this.articleId)
          status = 1
        }
        // 更新视图
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast.fail('服务器错误请稍后再试')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">
.collected{
  color: #e5645f;
}
</style>
